package org.sft.utils.wx;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.thoughtworks.xstream.XStream;

/**
 * @author benxi
 *
 * 2016年6月30日
 */
public class MessageUtil {

	/**
	 * 
	 */
	private MessageUtil() {
		// TODO Auto-generated constructor stub
	}
	public Map<String,String> XML2Map(HttpServletRequest request){
		Map<String, String> map=new HashMap<>();
		SAXReader reader=new SAXReader();
		InputStream is=null;
		try {
			is=request.getInputStream();
			Document doc=reader.read(is);
			Element root=doc.getRootElement();
			List<Element> eles=root.elements();
			for(Element e:eles){
				map.put(e.getName(), e.getText());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(is!=null){
			try {
				is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return map;
	}
	public static String textMessage2XML(TextMessage tmsg){
		XStream xStream=new XStream();
		
		return xStream.toXML(tmsg);
	}
}
